//
//  HCLoginPageView.m
//  HelloCall
//
//  Created by WangYongpeng on 13-10-15.
//  Copyright (c) 2013年 HC. All rights reserved.
//

#import "HCLoginPageViewController.h"
#import "HCMainViewController.h"
#import "HCNetworkManger.h"
#import "HCLoginInfoModel.h"

extern HCMainViewController* g_mainViewController;

@interface HCLoginPageViewController()<UITextFieldDelegate, HCNetworkObserver>

@property (nonatomic, retain)UITextField* userNameInput;
@property (nonatomic, retain)UITextField* pwdInput;

@end

@implementation HCLoginPageViewController

@synthesize userNameInput = m_userNameInput;
@synthesize pwdInput = m_pwdInput;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code
        UIImage* bgImage = [UIImage imageNamed:@"wallPaper.jpg"];
        UIImageView* bgImageView = [[[UIImageView alloc] initWithImage:bgImage] autorelease];
        CGRect rect = bgImageView.frame;
        if (!IOS_VERSION_7_OR_ABOVE) {//Eonman 改2
            rect.origin.y -= 20;
        }
        bgImageView.frame = rect;
        [self.view addSubview:bgImageView];
        
        rect = CGRectMake(50, 150, 70, 30);
        UILabel* userNameLabel = [[UILabel alloc] initWithFrame:rect];
        userNameLabel.text = @"用户名：";
        userNameLabel.textColor = [UIColor blackColor];
        userNameLabel.backgroundColor = [UIColor clearColor];
        [self.view addSubview:userNameLabel];
        [userNameLabel release];
        rect.origin.x += 70;
        rect.size.width = 150;
        self.userNameInput = [self CreateTextField:rect];
        self.userNameInput.placeholder = @"12345678901";//@"请输入用户名";
        [self.view addSubview:self.userNameInput];
        rect.origin.y += 30;
        rect.origin.x -= 70;
        rect.size.width = 100;
        
        UILabel* pwdLabel = [[UILabel alloc] initWithFrame:rect];
        pwdLabel.text = @"密    码：";
        pwdLabel.textColor = [UIColor blackColor];
        pwdLabel.backgroundColor = [UIColor clearColor];
        [self.view addSubview:pwdLabel];
        [pwdLabel release];
        rect.origin.x += 70;
        rect.size.width = 150;
        self.pwdInput = [self CreateTextField:rect];
        self.pwdInput.placeholder = @"1234567";//@"请输入密码";
        [self.view addSubview:self.pwdInput];
        
        
        rect.origin.y += 50;
        rect.size.width = 100;
        rect.origin.x = (self.view.frame.size.width - rect.size.width) / 2;
        UIButton* loginButton = [[UIButton alloc] initWithFrame:rect];
        [loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [loginButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:loginButton];
        [loginButton release];
        
        
        rect.origin.y = 400;
        rect.size.width = 100;
        rect.origin.x = 220;
        UIButton* testButton = [[UIButton alloc] initWithFrame:rect];
        [testButton setTitle:@"提醒测试" forState:UIControlStateNormal];
        [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [testButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        [testButton addTarget:self action:@selector(NotiftTestButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:testButton];
        [testButton release];
        
//        rect.origin.x += 150;
//        UIButton* loginButton = [[UIButton alloc] initWithFrame:rect];
//        [loginButton setTitle:@"登录" forState:UIControlStateNormal];
//        [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:loginButton];
//        [loginButton release];
        
        [[HCNetworkManger shareManger] addObserver:self];
    }
    return self;
}

- (void)dealloc
{
    [[HCNetworkManger shareManger] removeObserver:self];
    self.userNameInput = nil;
    self.pwdInput = nil;
    
    [super dealloc];
}

-(UITextField*)CreateTextField:(CGRect)rect
{
//    CGRect tRect = CGRectMake(55.0, 65, 150, 25);
    UITextField* tTextField = [[[UITextField alloc]initWithFrame:rect] autorelease];
    tTextField.borderStyle = UITextBorderStyleRoundedRect;
    tTextField.textColor = [UIColor blackColor];
    tTextField.font = [UIFont systemFontOfSize:20];
    tTextField.placeholder = @"<input text>";
    tTextField.backgroundColor = [UIColor whiteColor];
    tTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    tTextField.keyboardType = UIKeyboardTypeDefault;
    tTextField.returnKeyType = UIReturnKeyDone;
    tTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    tTextField.delegate = self;
    
    return tTextField;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)loginButtonAction
{
    NSString *userID = @"123456";
    [HCLoginInfoModel defaultModel].userID = userID;
    [HCLoginInfoModel defaultModel].userName = @"测试";
    [g_mainViewController showMainView];

    /*pause modify-----------原来通讯下面，暂时改为上面-----------------------
//    [self dismissViewControllerAnimated:YES completion:nil];
    NSString* userName = self.userNameInput.text;
    NSString* pwd = self.pwdInput.text;
    if (userName.length < 1 && pwd.length < 1)
    {
        userName = self.userNameInput.placeholder;
        pwd = self.pwdInput.placeholder;
    }
    [HCLoginInfoModel defaultModel].userPhoneNumber = userName;
    [[HCNetworkManger shareManger] loginWithUserName:userName pwd:pwd];
//    [[HCNetworkManger shareManger] loginWithUserName:@"-------" pwd:@"------"];
    
    
//    [g_mainViewController showMainView];
     pause modify---------------------------------------*/
}

- (void)NotiftTestButtonAction
{
    for (int i = 0; i < 5; i++)
    {
        UILocalNotification* notification = [[[UILocalNotification alloc] init] autorelease];
    
        if (notification)
        {
            NSDate* now = [NSDate new];
            notification.fireDate = [now dateByAddingTimeInterval:10 + i];
            notification.timeZone = [NSTimeZone defaultTimeZone];
            notification.alertBody = @"消息提醒测试";
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
    }
}

- (void)notifyNetworkMsg:(int)msgID data:(void*) data
{
    /*pause modify———————————————------
    switch (msgID)
    {
        case ELogin:
        {
            LoginRsp* resp = (LoginRsp*)data;
            
            if (!resp->headResp.success)
            {
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"用户名或密码输入错误，请重试。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
                [alert release];
                break;
            }
            
            NSString *userID = [NSString stringWithFormat:@"%d", resp->account_id];;
            [HCLoginInfoModel defaultModel].userID = userID;
            [HCLoginInfoModel defaultModel].userName = [NSString stringWithUTF8String:resp->name.data()];
            [g_mainViewController showMainView];
            break;
        }
        default:
            break;
    }
     pause modify———————————————————*/
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
